﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class admin_UpdateSellInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Main.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 42)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if (!IsPostBack )
        {
            SellInfo sellInfo = SellInfoManager.FindBySellInfoId(Convert.ToInt32(Request.QueryString["sid"]));
            txtClient.Text = sellInfo.Client.ClientName;
            hidClientId.Value = sellInfo.Client.ClientId.ToString();
            txtProduct.Text = sellInfo.Product.ProductName;
            hidProductId.Value = sellInfo.Product.Productid.ToString();
            txtSellDate.Text = sellInfo.SellDate.ToShortDateString();
            txtCount.Text = sellInfo.Count.ToString();
            txtPrice.Text = sellInfo.Price.ToString();
            txtAgio.Text = sellInfo.Agio.ToString();
            txtConsignmentUser.Text = sellInfo.ConsignmentUser;
            txtRemark.Text = sellInfo.Remark;
            hidSellUser.Value = sellInfo.ShellUser.UserId.ToString();
            this.RangeValidator2.MaximumValue = decimal.MaxValue.ToString();
            this.RangeValidator3.MaximumValue = int.MaxValue.ToString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SellInfo sellInfo = new SellInfo();
        sellInfo.SellInfoId=Convert.ToInt32(Request.QueryString["sid"]);
        TarreOA.TarreOAModels.Client client = new Client();
        client.ClientId = Convert.ToInt32(hidClientId.Value);
        sellInfo.Client = client;
        Product product = new Product();
        product.Productid =  Convert.ToInt32(hidProductId.Value);
        sellInfo.Product = product;
        if (txtSellDate.Text !="")
        {
            sellInfo.SellDate = Convert.ToDateTime(txtSellDate.Text.Trim());
        }
        if (txtCount.Text != "")
        {
            sellInfo.Count = Convert.ToInt32(txtCount.Text.Trim());
        }
        if (txtPrice.Text !="")
        {
            sellInfo.Price = Convert.ToInt32(txtPrice.Text.Trim());
        }
        if (txtAgio.Text !="")
        {
            sellInfo.Agio = Convert.ToInt32(txtAgio.Text.Trim());
        }
        sellInfo.ConsignmentUser = txtConsignmentUser.Text.Trim();
        sellInfo.Remark = txtRemark.Text.Trim();
        if (SellInfoManager.UpdateSellInfo(sellInfo))
        {
            Response.Redirect("ShowShellInfo.aspx?type=" + Request.QueryString["type"]+"&pageIndex="+Request.QueryString["pageIndex"]);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(),"","alert('修改失败！请重试！')",true);
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ShowShellInfo.aspx?type=" + Request.QueryString["type"]+"&pageIndex="+Request.QueryString["pageIndex"]);
    }
}
